using DC2010.Objects.Data;
using DC2010.Objects.Base;
using DC2010.Objects.Logic;

namespace DC2010.Objects
{
    public class DoorLogic : BaseLogic, IToogle
    {
        public DoorInfo Info { get; set; }
        public DoorGfx Gfx { get; set; }

        public DoorLogic(DoorData dd)
        {
            Info = new DoorInfo();
            Gfx = Root.I.DoorSystem.GetGfx(dd.GfxId);
            Data = dd;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aCaller">Caller will be in most cases key</param>
        /// <param name="aToogler">this will be in most cases keyhole</param>
        public void Toogle(BaseObject aCaller, BaseObject aToogler)
        {
            if (aCaller is ItemData)
            {
                ItemData i = (ItemData)aCaller;
                if (TryUnlock(i.Id))
                {
                    if (i.InfoId == Root.I.GameSystem.ItemInHand.InfoId)
                    {
                        Root.I.GameSystem.ItemInHand = null;
                    }
                }
            }

            //are all locks unlocked?
            if (Info.Keys.Count == 0)
            {
                //we can open
                Root.I.SoundQueue.AddToQueue("Door");
            }
        }

        public bool TryUnlock(string itemId)
        {
            string sKeyToRemove = "";

            foreach(string s in Info.Keys)
            {
                if (s == itemId)
                {
                    sKeyToRemove = s;
                    break;
                }
            }

            if (!string.IsNullOrEmpty(sKeyToRemove))
            {
                Info.Keys.Remove(sKeyToRemove);

                if (Info.ConsumeKey)
                {
                    Info.ConsumedKeys.Add(sKeyToRemove);
                    return true;
                }
            }
            return false;
               
        }

    }
}